//
//  TF1to16VotingAreaCell.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/7.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TF1to16VotingAreaCell: UITableViewCell {
    //投票状态  (投票中  已结束  已投票)
   @objc public let votingStatus = UILabel()
    //背景色
    @objc public let backgroundColorView = UIView()
    //投票标题
    @objc public let questionTitle = UILabel()
    //投票用户名字日期
    @objc public let votingUser = UILabel()
    //查看投票  ---投票
    @objc public let votingBehavior = UIButton()
    //分割线
    @objc public let dividingLine = UIView()
    //投票照片
    @objc public let voteImg = UIImageView()
    //点击了投票详情
    @objc public var voteBlock:TF1to16ExpressionCellmyBlock?
 
    @objc public var model:TFVoteData?
    {
        didSet
        {
            if (model != nil)
            {
                self.modelUI(model!)
            }
        }
    }
    func modelUI(_ model:TFVoteData)
    {
        if(model.info.label.count>0){
           self.questionTitle.text = model.info.label
        }else{
            self.questionTitle.text =  model.info.title
        }
        
        if (model.info.imageUrl.count > 0) {
            self.voteImg.sd_setImage(with: NSURL.init(string: model.info.imageUrl) as URL?)
           
            self.voteImg.isHidden = false
           
            self.voteImg.frame = CGRect(x: 20, y: 22, width: 27, height: 19)
            
            self.questionTitle.frame = CGRect(x: 56, y: 20, width: self.tf_Width-42-15-20-27, height: 24)
   
        } else {
            self.voteImg.isHidden = true
            self.voteImg.frame = CGRect(x: 0, y: 22, width: 10, height: 19)
          
            self.questionTitle.frame = CGRect(x: 20, y: 20, width: self.tf_Width-42-15, height: 24)
 
        }
        //还在投票
        if model.info.endTime == "0" || model.info.endTime.count == 0 {
            
      
            //未投过票
            if(model.voted==false){
                
       
                let string = NSMutableAttributedString.getAttributed(string: "投票中" , font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 255/255.0, green: 178/255.0, blue: 62/255.0, alpha: 1))
            self.votingStatus.attributedText = string
            self.votingStatus.textColor = UIColor(red: 255/255.0, green: 178/255.0, blue: 62/255.0, alpha: 1)
            
            self.votingStatus.backgroundColor = UIColor.clear
            
    
        self.votingBehavior.titleLabel!.font = UIFont.PingFang(size: 14)
                
                //"去投票"
        self.votingBehavior.setTitleColor(UIColor.white, for: .normal)
                self.votingBehavior.setTitle("去投票", for: .normal)
                
        self.votingBehavior.backgroundColor = UIColor(red: 77/255.0, green: 154/255.0, blue: 255/255.0, alpha: 1)
            
            }else{
                
                //已经投票
                
                let string = NSMutableAttributedString.getAttributed(string: "已投票", font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 114/255.0, green: 121/255.0, blue: 132/255.0, alpha: 1))
                
              
                self.votingStatus.attributedText = string;
                self.votingStatus.backgroundColor = UIColor.clear
                self.votingStatus.textColor = UIColor(red: 114/255.0, green: 121/255.0, blue: 132/255.0, alpha: 1)
                
                self.votingBehavior.setTitleColor(UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1), for: .normal)
                
                //查看
                self.votingBehavior.setTitle("查看", for: .normal)
               
                self.votingBehavior.backgroundColor = UIColor(red: 184/255.0, green: 189/255.0, blue: 196/255.0, alpha: 1)
                
            }
            
        }else{
            //已经结束投票了
        
            let string = NSMutableAttributedString.getAttributed(string: "已结束" , font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 114/255.0, green: 121/255.0, blue: 132/255.0, alpha: 1))
            
            
            self.votingStatus.attributedText = string;

            self.votingStatus.backgroundColor = UIColor.clear
            self.votingStatus.textColor = UIColor(red: 114/255.0, green: 121/255.0, blue: 132/255.0, alpha: 1)
           
            self.votingBehavior.setTitleColor(UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1), for: .normal)
            
            //查看
            self.votingBehavior.setTitle("查看", for: .normal)
           
            self.votingBehavior.backgroundColor = UIColor(red: 184/255.0, green: 189/255.0, blue: 196/255.0, alpha: 1)
        }
        
        //    @"陈助教（3-15）"
        var name = model.info.nickname as NSString

        if(name.length > 5)
        {
            
            name =  name.substring(with:NSRange(location: 0, length: 5)) as NSString
            name =   (name as String) + "..." as NSString

        }
        if (model.info.startTime.count>0) {
            
            let model_info_startTime:NSString = model.info.startTime as NSString
            let b = model_info_startTime.components(separatedBy: " ")
            
            
    
            if(b.count>=1){
                let time:NSString = b[0] as NSString
                
                 let bb = time.components(separatedBy: "-")

                if (bb.count>2) {
                    let dd:NSString =  bb[1] as NSString
                    let cc:NSString =  bb[2] as NSString
                    if (cc.length>0) {
                        
                        let ee = (dd as String) + "-" + (cc as String)
                        
                        
                        name = (name as String) + " " + ee as NSString
                        
                        
                        name = (name as String) + " " + ((b[1] as NSString) as String) as NSString
                    }
                   
                    
                    
                }else{
                    
                    name =  (name as String) + " " + model.info.startTime as NSString
                }
            
            }
        }
        
        let votingUserString1 = NSMutableAttributedString.getAttributed(string: name as String,  font: UIFont.PingFang(size: 14), foregroundColor: UIColor(red: 193/255.0, green: 199/255.0, blue: 214/255.0, alpha: 1))
                                                                      

        
        self.votingUser.attributedText = votingUserString1
    }
    @objc public var tf_Width:CGFloat = 0

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
       
        self.backgroundColorView.backgroundColor = UIColor(red: 28/255.0, green: 31/255.0, blue: 35/255.0, alpha: 0.98)
        self.contentView.addSubview(self.backgroundColorView)
       
       //投票状态  (投票中  已结束  已投票)
        self.votingStatus.textAlignment = .center
       self.votingStatus.numberOfLines = 0
        self.backgroundColorView.addSubview(self.votingStatus)

       self.questionTitle.frame = CGRect(x: 20, y: 20, width: 57, height: 24)
       self.questionTitle.numberOfLines = 0;
       self.backgroundColorView.addSubview(self.questionTitle)
       self.questionTitle.font = UIFont.PingFang(size: 16)
       self.questionTitle.text = "我是有底线的哦..."
       self.questionTitle.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
 
       self.votingUser.numberOfLines = 0;
        self.backgroundColorView.addSubview(self.votingUser)
       
       self.voteImg.isHidden = true
        self.backgroundColorView.addSubview(self.voteImg)

   //查看投票  ---投票
     self.votingBehavior.titleLabel!.font = UIFont.PingFang(size: 14)
    self.votingBehavior.layer.cornerRadius = 15
    self.votingBehavior.addTarget(self, action: #selector(self.votingBehaviorclick), for: .touchUpInside)
    
    self.dividingLine.backgroundColor = UIColor(red: 63/255.0, green: 67/255.0, blue: 74/255.0, alpha: 1)
        
    self.dividingLine.layer.cornerRadius = 3
    self.backgroundColorView.addSubview(self.dividingLine)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   @objc func votingBehaviorclick()
    {
        
        if self.model != nil && self.voteBlock != nil {
            self.voteBlock!(self.model!.vid)
        }
       
    }
  
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        self.backgroundColorView.frame = self.bounds
        
        //投票状态
        let votingStatusWidth:CGFloat = 42
        let votingStatusHeight:CGFloat = 20
        let votingStatusX:CGFloat = self.backgroundColorView.frame.size.width - votingStatusWidth - 20//10是间距
        let votingStatusY:CGFloat = 21
        self.votingStatus.frame = CGRect(x: votingStatusX, y: votingStatusY, width: votingStatusWidth, height: votingStatusHeight)
        
        let votingBehaviorWidth:CGFloat = 60
        let votingBehaviorHeight:CGFloat = 30
        let votingBehaviorX:CGFloat =  self.backgroundColorView.frame.size.width - votingBehaviorWidth - 15
        let votingBehaviorY:CGFloat = self.backgroundColorView.frame.size.height - votingBehaviorHeight - 13.5
        //查看投票  ---投票
        self.votingBehavior.frame = CGRect(x: votingBehaviorX, y: votingBehaviorY, width: votingBehaviorWidth, height: votingBehaviorHeight)
        
        //投票用户名字
        self.votingUser.frame = CGRect(x: 20, y: 20+24+10, width: self.backgroundColorView.frame.size.width - 40, height: 20)
       
        self.votingUser.backgroundColor = UIColor.clear
        let dividingLineW:CGFloat = self.backgroundColorView.frame.size.width - (20*2)
        let dividingLineH:CGFloat = 0.5
        let dividingLineX:CGFloat = 20
        let dividingLineY:CGFloat = self.backgroundColorView.frame.size.height - dividingLineH;
        self.dividingLine.frame = CGRect(x: dividingLineX, y: dividingLineY, width: dividingLineW, height: dividingLineH)
        
    }

}
